﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace Planetwork
{
    public partial class MyInfo : System.Web.UI.Page
    {
        static string conStr = ConfigurationManager.AppSettings["conStr"];
        SqlConnection mycon = new SqlConnection(conStr);
        protected void Page_Load(object sender, EventArgs e)
        {
            if(Session["name"] == null)
            {
                Response.Redirect("Login.aspx?ReturnUrl=" + Request.RawUrl);
            }

            if (!IsPostBack)
            {
                //MyInfo           
                string sql = "select *from Account where UserName = @userName";
                SqlDataAdapter mydat = new SqlDataAdapter(sql, mycon);
                mydat.SelectCommand.Parameters.AddWithValue("@userName", Session["name"].ToString());
                DataSet myds = new DataSet();
                mycon.Open();
                mydat.Fill(myds);
                mycon.Close();
                this.UserName.Text = myds.Tables[0].Rows[0]["UserName"].ToString();
                this.Email.Text = myds.Tables[0].Rows[0]["Email"].ToString();
                this.Name.Text = myds.Tables[0].Rows[0]["Name"].ToString();
                this.Password.Text = myds.Tables[0].Rows[0]["UserPassword"].ToString();
                this.Birth.Text = myds.Tables[0].Rows[0]["Birth"].ToString();
                this.Biog.Text = myds.Tables[0].Rows[0]["Biography"].ToString();

                //MessagesManager
                MessageBind();

                //ImagesManager
                ImageBind();
               
            }
        }

        /// <summary>
        /// 保存管理信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void SavaMyInfo_Click(object sender, EventArgs e)
        {
            string sql = @"Update Account set Email = @email, Name = @name, UserPassword = @password, 
                            Birth = @birth, Biography = @biog where UserName = @userName";
            SqlCommand mycmd = new SqlCommand(sql, mycon);
            mycmd.Parameters.AddWithValue("@email", this.Email.Text.Trim());
            mycmd.Parameters.AddWithValue("@name", this.Name.Text.Trim());
            mycmd.Parameters.AddWithValue("@password", this.Password.Text.Trim());
            mycmd.Parameters.AddWithValue("@birth", this.Birth.Text.Trim());
            mycmd.Parameters.AddWithValue("@biog", this.Biog.Text.Trim());
            mycmd.Parameters.AddWithValue("@userName", Session["name"].ToString());
            mycon.Open();
            try
            {
                mycmd.ExecuteNonQuery();
                mycon.Close();
                Response.Write("<script>alert('修改成功');</script>");              
            }
           catch
            {
                mycon.Close();
                Response.Write("<script>alert('修改失败');</script>");
            }          
        }

        /// <summary>
        /// 留言模块数据源
        /// </summary>
        void MessageBind()
        {
            string sql = "select  d.Title, r.ReviewDate, r.Content, r.ID  from Reviews r ,DefaultImage d where r.ParentID = d.ID  and r.Author = @author";
            SqlDataAdapter mydat = new SqlDataAdapter(sql, mycon);
            mydat.SelectCommand.Parameters.AddWithValue("@author", Session["name"].ToString());
            DataSet myds = new DataSet();

            mycon.Open();
            mydat.Fill(myds);
            mycon.Close();

            if(myds.Tables[0].Rows.Count > 0)
            {
                this.showMessage.DataSource = myds.Tables[0];
                this.showMessage.DataBind();
            }
        }

        /// <summary>
        /// 管理留言删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void showMessage_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if(e.CommandName == "del")
            {
                string sql = "delete Reviews where ID=" + e.CommandArgument.ToString();
                SqlCommand mycmd = new SqlCommand(sql, mycon);

                mycon.Open();
                int i = mycmd.ExecuteNonQuery();
                mycon.Close();

                if (i > 0)
                {
                    Response.Write("<script>alert('删除成功!');</script>");
                    MessageBind();
                }
            }
        }

        /// <summary>
        /// 管理相片数据源
        /// </summary>
        void ImageBind()
        {
            string sql = "select i.ID, i.ImageData, i.UploadDate from Account a, Images i where a.UserName = i.Author and i.Author = @author";
            SqlDataAdapter mydat = new SqlDataAdapter(sql, mycon);
            mydat.SelectCommand.Parameters.AddWithValue("@author", Session["name"].ToString());
            DataSet myds = new DataSet();

            mycon.Open();
            mydat.Fill(myds);
            mycon.Close();

            if(myds.Tables[0].Rows.Count > 0)
            {
                this.showImageInfo.DataSource = myds.Tables[0];
                this.showImageInfo.DataBind();
            }
        }


        /// <summary>
        /// 管理相片删除事件
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void showImageInfo_DeleteCommand(object source, DataListCommandEventArgs e)
        {
            string sql = "delete from Images where ID=@id";
            SqlCommand mycmd = new SqlCommand(sql, mycon);
            mycmd.Parameters.AddWithValue("@id", e.CommandArgument.ToString());

            mycon.Open();
            try
            {
                mycmd.ExecuteNonQuery();
                mycon.Close();
                Response.Write("<script>alert('删除成功');</script>");
                ImageBind();
            }
            catch
            {
                Response.Write("<script>alert('删除失败');</script>");
            }
        }
    }
}